home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_oth / pcrng / launi.asm next >
Assembly Source File  |  1990-06-28  |  15KB  |  368 lines

  1.               page ,132
  2. ;
  3. ;***********************************************************************
  4. ;* Here are data declarations for use by RINIT, IVNI, IUNI, VNI, UNI.  *
  5. ;* All routines are written for use with LAHEY Fortran Compiler        *
  6. ;* Version 2.22 and above.                                             *
  7. ;*                                                                     *
  8. ;*   Authors: G. Marsaglia, B. Narasimhan and Arif Zaman               *
  9. ;*                 Supercomputer Computations Research Institute       *
  10. ;*                         and                                         *
  11. ;*                 Department of Statistics                            *
  12. ;*                 Florida State University                            *
  13. ;*                 Tallahassee, Fl 32306-3033.                         *
  14. ;***********************************************************************
  15. ;
  16.               .MODEL     small                        ;Use small memory model
  17.               TITLE      'A Random Number Generator for PC's.'
  18. ;
  19. ;---------------Equates-----------------
  20. ;
  21. arg_offset    equ            6                        ;Offset of argument.
  22. c_low         equ       0cbb1h
  23. c_high        equ          74h                        ;C = 7654321 initially.
  24. cons_low      equ        55e5h
  25. cons_high     equ        159ah                        ;Constant = 362436069.
  26. no_of_bits    equ           32
  27. top_of_list   equ          168
  28. init_i_val    equ  top_of_list
  29. init_j_val    equ           84
  30. ;
  31. datseg        segment     para  
  32. ;
  33. ; Table for Generator (Default values).
  34. ;
  35. unitabl       dd         0F83CEE7Bh
  36.               dd         0A83E5AD3h
  37.               dd          036200BBh
  38.               dd         0FA5764F6h
  39.               dd         0A13CBFC4h
  40.               dd          565A191Eh
  41.               dd          14D4CCFBh
  42.               dd          7F5AD22Ch
  43.               dd          03528F2Eh
  44.               dd         0E81E32DDh
  45.               dd          71C47276h
  46.               dd         0AA0F8045h
  47.               dd          3C3F1C78h
  48.               dd         0E8CE101Dh
  49.               dd         0CCC12691h
  50.               dd          47196DBFh
  51.               dd          074A6DFFh
  52.               dd          03FB675Eh
  53.               dd          60436236h
  54.               dd          072F0247h
  55.               dd          0C7E9185h
  56.               dd         0CE579EAEh
  57.               dd          01864E96h
  58.               dd         0A526C5C6h
  59.               dd         0F582EB0Dh
  60.               dd         0AFE827F4h
  61.               dd          55CED836h
  62.               dd          26124C49h
  63.               dd          7049AEE1h
  64.               dd          49552795h
  65.               dd         0D1602ED6h
  66.               dd          051C65CEh
  67.               dd         0AEF3CC37h
  68.               dd         0A83880ABh
  69.               dd         0AE7EE06Ah
  70.               dd         0D64D988Bh
  71.               dd          0A96BC73h
  72.               dd         0ECEF4297h
  73.               dd          6C18300Dh
  74.               dd          22F3A897h
  75.               dd         0B4D760ADh
  76.               dd         0AC838383h
  77.               dd         0FD04E68Fh
  78. ;
  79. i             dw               164
  80. j             dw                80                    ;Indices I and J.
  81. c             dd         0EADA75CCh                   ;Constant C.
  82. mod169        db               169                  
  83. mod179        db               179                  
  84. multiplier    db                53
  85. carry         db                 1                    ;Carry bit (Default).
  86. stored_val    dd          122a70c3h
  87. datseg        ends
  88. ;
  89. ;***********************************************************************
  90. ;* Subroutine RINIT takes an 8-digit number as argument. Use as        *
  91. ;*                  CALL RINIT(I)                                      *
  92. ;*                                                                     *
  93. ;*  Purpose: Seeds the table and prepares pointers for use by other    *
  94. ;*           routines.                                                 *
  95. ;*                                                                     *
  96. ;* This is set up for use with LAHEY Fortran Compiler Version 2.22 and *
  97. ;* above.                                                              *
  98. ;*                                                                     *
  99. ;*   Authors: G. Marsaglia, B. Narasimhan and A. Zaman                 *
  100. ;*            Supercomputer Computations Research Institute            *
  101. ;*                    and                                              *
  102. ;*            Department of Statistics                                 *
  103. ;*            Florida State University                                 *
  104. ;*            Tallahassee, Fl 32306-3033.                              *
  105. ;***********************************************************************
  106. ;
  107.               .CODE
  108. rseg          segment
  109. ;
  110.               assume cs:rseg, ds:datseg
  111. rinit         proc       Far
  112.               public     rinit
  113. ;
  114. ; Usual LAHEY stuff....
  115. ;
  116.               push       bp                           ;Save caller's BP.
  117.               mov        bp,sp                        ;Ready to address.
  118. ;
  119.               mov        ax,datseg                    ;DATA segment.
  120.               mov        ds,ax
  121. ;
  122. ; Load parameter and strip two digits at a time to get four seed values.
  123. ;
  124.               les        si,DWORD PTR[bp].arg_offset
  125.               mov        ax,Word Ptr es:[si]
  126.               mov        dx,Word Ptr es:[si]+2
  127.               mov        cx,10000
  128.               div        cx
  129.               mov        cl,100
  130.               div        cl
  131.               mov        bx,ax                        ;BH = K, BL = L.
  132.               mov        ax,dx
  133.               div        cl                    
  134.               mov        dl,ah                
  135.               mov        dh,al                        ;DH = J, DL = I.        
  136. ;
  137. ; Add one to all to make sure they are non-zero.
  138. ;
  139.               inc        dl
  140.               inc        dh
  141.               inc        bh
  142.               inc        bl
  143. ;
  144. ; DL = I, DH = J, BL = L, BH = K.
  145. ;
  146.               xor        si,si                        ;Table Index.
  147. labl01:
  148. ;
  149. ; Result will be in [BP,DI].
  150. ;
  151.               xor        di,di
  152.               xor        bp,bp
  153.               mov        cx,no_of_bits                ;Bit counter.
  154. labl02:
  155.               shl        di,1              
  156.               rcl        bp,1                         ;Result *= 2.
  157.               mov        al,dh                        ;AL = J.
  158.               mul        bh                           ;AX = J * K.
  159.               div        mod179
  160.               mov        al,ah                        ;AL = AX mod 179.
  161.               mul        dl                           ;AX = AX * I.
  162.               div        mod179
  163.               mov        dl,dh                        ;I = J.
  164.               mov        dh,bh                        ;J = K.
  165.               mov        bh,ah                        ;K = new K.
  166. ;
  167.               mov        al,bl                        ;AL = L.
  168.               mul        multiplier
  169.               inc        ax
  170.               div        mod169
  171.               mov        bl,ah                        ;L = 53*L+1 mod 169.
  172. ;
  173.               mov        al,bl                        ;AL = new L.
  174.               mul        bh                           ;AX = L * K.
  175.               and        ax,63                        ;Mod 64.
  176.               cmp        ax,32
  177.               jl         labl03
  178.               inc        di                           ;Set low bit.
  179. labl03:
  180.               loop       labl02          
  181. ;
  182.               mov        WORD PTR unitabl[si],di       
  183.               mov        WORD PTR unitabl[si+2],bp    ;Save value.
  184.               add        si,4                         ;Bump pointer.
  185.               cmp        si,172                       ;Seeded 86 entries?
  186.               jne        labl01
  187. ;
  188. ; Set indices.
  189. ;
  190.               mov        i,init_i_val
  191.               mov        j,init_j_val
  192. ;
  193. ; Set carry to zero.
  194. ;
  195.               mov        carry,0
  196. ;
  197. ; Set constants.
  198. ;
  199.               mov        WORD PTR c,c_low
  200.               mov        WORD PTR c+2,c_high
  201. ;
  202. ; Ready the first value.
  203. ;
  204.               mov        ax,offset stored_val
  205.               mov        bx,seg stored_val
  206.               push       bx
  207.               push       ax
  208.               call       Far Ptr ivni
  209.               pop        ax
  210.               pop        ax
  211. ;
  212. ; Epilogue...
  213. ;
  214.               pop        bp
  215.               ret
  216. rinit         endp
  217. ;
  218. ;***********************************************************************
  219. ;* This proc contains the following functions.                         *
  220. ;*                                                                     *
  221. ;*  IVNI     : A function that returns a signed random integer between *
  222. ;*             -2**31 and 2**31-1.                                     *
  223. ;*  IUNI     : A function that returns a positive random integer       *
  224. ;*             between 0 and 2**31-1.                                  *
  225. ;*  VNI      : A function that returns a signed random uniform between *
  226. ;*             -1 and 1.                                               *
  227. ;*  UNI      : A function that returns a positive random uniform       *
  228. ;*             between 0 and 1.                                        *
  229. ;*                                                                     *
  230. ;*  All routines mix a subtract-with-borrow generator and an           *
  231. ;*  arithmetic sequence:                                                          *
  232. ;*              x(n) = x(n-22) - x(n-43) - carry mod 2**32-5.          *
  233. ;*              c = c + 362436069 mod 2**32.                           *
  234. ;*              result = x(n) - c mod 2**32.                           *
  235. ;*                                                                     *
  236. ;* This is set up for use with LAHEY Fortran Compiler Version 2.22 and *
  237. ;* above.                                                              *
  238. ;*                                                                     *
  239. ;*   Authors: G. Marsaglia, B. Narasimhan and A. Zaman                 *
  240. ;*            Supercomputer Computations Research Institute            *
  241. ;*                    and                                              *
  242. ;*            Department of Statistics                                 *
  243. ;*            Florida State University                                 *
  244. ;*            Tallahassee, Fl 32306-3033.                              *
  245. ;***********************************************************************
  246. ;
  247.               assume cs:rseg, ds:datseg
  248. ivni          proc       Far                          ;First entry point.
  249.               public     ivni
  250.               mov        ax,datseg                 
  251.               mov        ds,ax                     
  252.               mov        ax,Word Ptr stored_val
  253.               mov        bx,Word Ptr stored_val+2
  254.               jmp        save_and_get_next
  255. ;
  256. iuni          label      Far                          ;Second entry point.
  257.               public     iuni
  258.               mov        ax,datseg                 
  259.               mov        ds,ax                     
  260.               mov        ax,Word Ptr stored_val
  261.               mov        bx,Word Ptr stored_val+2
  262.               and        bx,7fffh                     ;Mask sign bit.
  263.               jmp        save_and_get_next
  264. ;
  265. vni           label      Far                          ;Third entry point.
  266.               public     vni
  267.               mov        ax,datseg
  268.               mov        ds,ax                     
  269.               mov        ax,Word Ptr stored_val
  270.               mov        bx,Word Ptr stored_val+2
  271.               mov        di,ax                        ;Sign is last bit.
  272.               jmp        normalize
  273. ;
  274. uni           label      Far                          ;Fourth entry point.
  275.               public     uni
  276.               mov        ax,datseg                 
  277.               mov        ds,ax
  278.               mov        ax,Word Ptr stored_val
  279.               mov        bx,Word Ptr stored_val+2
  280.               xor        di,di                        ;Sign should be zero.
  281. ;
  282. ; Normalize the number.
  283. ;
  284. normalize:
  285.               mov        cx,no_of_bits                ;Bit counter.
  286. labl04:
  287.               shl        ax,1                         ; Shift left.
  288.               rcl        bx,1
  289.               jc         labl05
  290.               loop       labl04
  291.               jmp        save_and_get_next            ;Store zero.
  292. labl05:
  293.               add        cx,94
  294.               mov        al,ah
  295.               mov        ah,bl
  296.               mov        bl,bh
  297.               mov        bh,cl
  298.               test       di,1
  299.               jz         labl06
  300.               stc
  301. labl06:
  302.               rcr        bx,1
  303.               rcr        ax,1
  304. ;
  305. save_and_get_next:
  306.               push       bp
  307.               mov        bp,sp
  308.               les        si,DWord Ptr [bp].arg_offset ;Load result address.
  309.               mov        Word Ptr es:[si],ax
  310.               mov        Word Ptr es:[si]+2,bx
  311.               pop        bp
  312. ;
  313.               mov        di,j                         ;Load index j.
  314.               mov        cx,Word Ptr unitabl[di]
  315.               mov        dx,Word Ptr unitabl[di]+2    ;U(j) in DX, CX.
  316. ;
  317.               sub        di,4                         ;j <- j - 4.
  318.               jns        update_j
  319.               mov        di,168
  320. update_j:
  321.               mov        j,di
  322. ;
  323.               mov        si,i                         ;Load index i.
  324.               mov        ah,carry                     ;Load carry.
  325.               sahf
  326.               sbb        cx,Word Ptr unitabl[si]
  327.               sbb        dx,Word Ptr unitabl[si]+2    ;U(j)-U(i) in DX, CX.
  328.               lahf
  329.               mov        carry,ah                     ;Save carry.
  330.               jnc        dont_modify
  331. ;
  332. ;Negative.
  333. ;
  334.               sub        cx,5                         ;Subtract 5.
  335.               sbb        dx,0
  336. dont_modify:
  337. ;
  338. ;Save result.
  339. ;
  340.               mov        Word Ptr unitabl[si],cx
  341.               mov        Word Ptr unitabl[si]+2,dx
  342. ;
  343.               sub        si,4                         ;i <- i - 4.
  344.               jns        update_i
  345.               mov        si,168
  346. update_i:
  347.               mov        i,si
  348.  
  349. ;
  350. ;Prepare arithmetic sequence.
  351. ;
  352.               mov        ax,Word Ptr c
  353.               mov        bx,Word Ptr c+2
  354.               sub        ax,cons_low
  355.               sbb        bx,cons_high
  356.               mov        Word Ptr c,ax
  357.               mov        Word Ptr c+2,bx
  358. ;
  359.               sub        cx,ax
  360.               sbb        dx,bx
  361.               mov        Word Ptr stored_val,cx
  362.               mov        Word Ptr stored_val+2,dx     ;Save for next time.
  363.               ret
  364. ivni          endp
  365. ;
  366. rseg          ends
  367.               END
  368.